package vashion.azeroth.core.pojo;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;


/**
 * attribute解析相关的Util
 *
 * 创建时间：2014-06-17
 */
public class AttributeUtils {

    static Log log = LogFactory.getLog(AttributeUtils.class);

    public static Map<String/**key*/, String/**value*/> getAttributeKeyToValueMap(String attributes){
        Map<String, String> amap = new LinkedHashMap<String, String>();
        if (StringUtils.isNotBlank(attributes)) {
            String[] attributeArray = attributes.split(";");
            for (String attribute : attributeArray) {
                String[] kvs = attribute.split(":");
                if (kvs != null && kvs.length == 2) {
                    amap.put(kvs[0], kvs[1]);
                }
            }
        }
        return amap;
    }

    public static Map<String/**value*/, String/**key*/> getAttributeValueToKeyMap(String attributes){
        Map<String/**key*/, String/**value*/> amap1 = getAttributeKeyToValueMap(attributes);
        Map<String/**value*/, String/**key*/> amapReturn = new LinkedHashMap<String, String>();
        if( amap1 != null ){
            for( Entry<String/**key*/, String/**value*/> entry : amap1.entrySet() ){
                amapReturn.put(entry.getValue(), entry.getKey());
            }
        }
        return amapReturn;
    }

    public static String getSingleAttribute(String attributes, String keyName) {
        if (StringUtils.isBlank(keyName) || StringUtils.isBlank(attributes)) {
            return null;
        }
        Map<String, String> amap = getAttributeKeyToValueMap(attributes);
        return amap.get(keyName);
    }

    public static String setSingleAttribute(String attributes, String key, String value) {
        if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
            return attributes;
        }
        Map<String, String> amap = getAttributeKeyToValueMap(attributes);
        amap.put(key, value);
        StringBuffer sb = new StringBuffer();
        for (String skey : amap.keySet()) {
            sb.append(";");
            sb.append(skey);
            sb.append(":");
            sb.append(amap.get(skey));
        }

        String attributesReturn = sb.toString();
        attributesReturn = attributesReturn.replaceFirst(";", "");
        return attributesReturn;
    }


    public static void main(String[] args){
        String q = "AND sd AND gmtCreate:[20140416155445 TO 20140416155445] AND keyWord:abc AND country:(中国香港 OR 希腊)";
        //  q = "AND sd AND gmtCreate:[s]";
        if(q.contains("gmtCreate:[")){
            String oldGmtCreateParam = null;
            String queryGmtCreate  = q.substring(q.indexOf("gmtCreate:["));
            if(StringUtils.isNotBlank(queryGmtCreate)){
                oldGmtCreateParam = queryGmtCreate.substring(0, queryGmtCreate.indexOf("]")+1);
                System.out.println("oldGmtCreateParam = "+oldGmtCreateParam);

                if(StringUtils.isNotBlank(oldGmtCreateParam)){
                    String rangeValue  = oldGmtCreateParam.split("\\[")[1];
                    rangeValue = rangeValue.substring(0, rangeValue.indexOf("]"));
                    System.out.println("rangeValue = "+rangeValue);
                    String[] ranges = rangeValue.split(" TO ");
                    if(ranges.length == 2){
                        String newGmtCreateParam = "gmtCreate:[aa TO BB]";
                        System.out.println("newGmtCreateParam = "+newGmtCreateParam);

                        q = q.replace(oldGmtCreateParam, newGmtCreateParam);
                    }


                }
            }


            System.out.println(new Date().getTime());


//String[]
//            if(qs.length>0){
//
//           }
//           for(String str : qs){
//               if(str.contains("gmtCreate:[")){
//                   System.out.println(str);
//
//                   break;
//               }
//           }


        }

    }

    static String  getEndTime(String q){


        return null;

    }

}
